<?

define("TABLE_BORDER",0);
define("MENU_TABLE_BORDER",0);
define("FACE_TABLE_BORDER",0);

class Face {
    private $status_line;
    private $cluster_id;
    private $database_id;
    private $schema_id;
    private $body;

    public function __construct() {
	$this->status_line="</div>";
	$this->cluster_id=false;
	$this->database_id=false;
	$this->schema_id=false;
    }

    public function draw() {
	echo "<div style=\"background-color: #DDD\"><b><a href=".$_SERVER['PHP_SELF'].">Home</a> | <a href=".$_SERVER['PHP_SELF']."?level=null&action=cluster_add>Add New Host</a></b>"; // Begin
	echo $this->_status_line(); // Status Line
	echo "<table border=".FACE_TABLE_BORDER." width=100%><tr valign=top><td width=21%>"; // Left frame start
	echo $this->_menu(); //Left Frame (Complete menu)
	echo "</td><td>"; // End of Left Frame. Right Frame start
	echo (isset($this->body) ? $this->body : ''); // Right Frame. Main Content
	echo "</td></tr></table>"; // End
    }

    public function flush_level() {
	$this->cluster_id=false;
	$this->database_id=false;
	$this->schema_id=false;
	$this->body='';
    }

    public function set_body($body) {
	$this->body=$body;
    }

    public function set_level($level,$id) {
	switch ($level) {
	case 'cluster':
	    $this->cluster_id=$id;
	    break;
	case 'database':
	    $this->database_id=$id;
	    $dn=new DatabaseName($id);
	    $this->cluster_id=$dn->get_field('hc_id');
	    break;
	case 'schema':
	    $this->schema_id=$id;
	    $sn=new SchemaName($id);
	    $this->database_id=$sn->get_field('dn_id');
	    $dn=new DatabaseName($this->database_id);
	    $this->cluster_id=$dn->get_field('hc_id');
	    break;
	default:
	    $this->flush_level();
	    break;
	}
    }






    private function _status_line() {
	$string=$this->status_line;
	return $string;
    }

    private function _menu() {
	$menu="<table border=".MENU_TABLE_BORDER."><tr valign=top><td>";
	$menu.=$this->_clusters();
	$menu.="</td><td>";
	$menu.=$this->_databases();
	$menu.="</td><td>";
	$menu.=$this->_schemas();
	$menu.="</td></tr></table>";
	return $menu;
    }

    private function _clusters() {
	$sql=new PgMonSQL;
	$clusters=new GenericObjectCollection('host_cluster','HostCluster','id');
	$sql->select_c("SELECT id FROM host_cluster WHERE alive ORDER BY hostname");
	foreach ($sql->get_result() as $hc_id) {
	    $clusters->add_id($hc_id['id']);
	}

	$string="Clusters<br><table border=".TABLE_BORDER.">";
	foreach ($clusters->get_object_array() as $hc) {
	    if ($hc->get_id() != $this->cluster_id) {
		$string.="<tr>";
	    } else {
		$string.="<tr bgcolor=#CCC>";
	    }
	    $string.="<td><a href=".$_SERVER['PHP_SELF']."?level=cluster&action=view&id=".$hc->get_id().">".$hc->get_field('hostname')."</a>
	    </td><td>".($hc->get_field('is_master')=='t' ? '<b>M</b>' : 's')."</td><td>".($hc->get_field('observable')=='t' ? '<b>O</b>' : 's')."</td></tr>";
	}
	$string.="</table>";
	return $string;
    }

    private function _databases() {
	$string='';
	if ($this->cluster_id) {
	    $string="Databases<br><table border=".TABLE_BORDER.">";
	    $hc=new HostCluster($this->cluster_id);
	    if ($hc->get_field('is_master')=='t') {
		$hc->initiate_composit('database_name',array('alive'=>'t'));
		foreach ($hc->get_composit('database_name') as $db) {
		    if ($db->get_id() != $this->database_id) {
			$string.="<tr>";
		    } else {
			$string.="<tr bgcolor=#CCC>";
		    }
		    $string.="<td><a href=".$_SERVER['PHP_SELF']."?level=database&action=view&id=".$db->get_id().">".$db->get_field('db_name')."</a>
		    </td><td>".($db->get_field('observable')=='t' ? '<b>O</b>' : 's')."</td></tr>";
		}
	    }
	    $string.="</table>";
	}
	return $string;
    }

    private function _schemas() {
	$string='';
	if ($this->database_id) {
	    $string="Schemas<br><table border=".TABLE_BORDER.">";
	    $dn=new DatabaseName($this->database_id);
	    $dn->initiate_composit('schema_name',array('alive'=>'t'));
	    foreach ($dn->get_composit('schema_name') as $sch) {
		if ($sch->get_id() != $this->schema_id) {
		    $string.="<tr>";
		} else {
		    $string.="<tr bgcolor=#CCC>";
		}
		$string.="<td><a href=".$_SERVER['PHP_SELF']."?level=schema&action=view&id=".$sch->get_id().">".$sch->get_field('sch_name')."</a>
		</td><td>".($sch->get_field('observable')=='t' ? '<b>O</b>' : 's')."</td></tr>";
	    }
	    $string.="</table>";
	}
	return $string;
    }

}

?>
